﻿using System;
using System.Linq;
using System.Collections.Generic;
using BotonJXC.Entity;
using WarehouseDAO = BotonJXC.DAO.WarehouseDAO;
using SysContext = BotonJXC.DAO.SysContext;
using BotonJXC.Util;
using NHibernate.Criterion;

namespace BotonJXC.BLL
{
    public class WarehouseBLL : BaseBLL<WarehouseDAO, Warehouse, int>
    {
        public WarehouseBLL()
        {
        }

        /// <summary>
        /// 查询所有大仓库地
        /// </summary>
        /// <returns></returns>
        public IList<Warehouse> FindBigWarehouses()
        {
            return Dao.Find(Restrictions.Eq("WarehouseType", (short)4));
        }

        /// <summary>
        /// 带数据权限查询所有仓库地
        /// </summary>
        /// <returns></returns>
        public IList<Warehouse> FindByUserAll()
        {
            List<ICriterion> criterions = new List<ICriterion>();
            Junction junction = Restrictions.Conjunction();
            Disjunction disjunction = Restrictions.Disjunction();
            int[] storeIds = SysContext.GetCurrentUserDataAuthStoreId();
            if (storeIds.Length > 0)
            {
                disjunction.Add(Restrictions.And(Restrictions.Eq("WarehouseType", (short)1), Restrictions.In("AttriToId", storeIds)));
            }
            int[] depIds = SysContext.GetCurrentUserDataAuthDepId();
            if (depIds.Length > 0)
            {
                disjunction.Add(Restrictions.And(Restrictions.Eq("WarehouseType", (short)2), Restrictions.In("AttriToId", depIds)));
            }
            criterions.Add(junction);
            criterions.Add(disjunction);
            return Dao.Find(criterions.ToArray());
        }

        public PageModel<Warehouse> FindByPage(string warehouseName, short warehouseLevel, short warehouseType, int start, int count)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            Junction junction = Restrictions.Conjunction();
            Disjunction disjunction = Restrictions.Disjunction();
            if (!string.IsNullOrEmpty(warehouseName))
            {
                junction.Add(Restrictions.Like("WarehouseName", warehouseName, MatchMode.Anywhere));
            }

            if (warehouseType > 0)
            {
                junction.Add(Restrictions.Eq("WarehouseType", warehouseType));
            }
            else
            {
                junction.Add(Restrictions.In("WarehouseType", new short[] { 1, 2 }));
            }

            if (warehouseLevel > 0)
            {
                junction.Add(Restrictions.Eq("WarehouseLevel", warehouseLevel));
            }

            int[] storeIds = SysContext.GetCurrentUserDataAuthStoreId();
            int[] sdepIds = new int[0];
            if (storeIds.Length > 0)
            {
                disjunction.Add(Restrictions.And(Restrictions.Eq("WarehouseType", (short)1), Restrictions.In("AttriToId", storeIds)));
                sdepIds = new DepartmentBLL().FindByStore(storeIds).Select(d => d.DepId).ToArray();
            }

            int[] depIds = SysContext.GetCurrentUserDataAuthDepId();

            if (sdepIds != null && sdepIds.Length > 0)
            {
                int[] ndepIds = new int[sdepIds.Length + depIds.Length];
                Array.Copy(sdepIds, 0, ndepIds, 0, sdepIds.Length);
                Array.Copy(depIds, 0, sdepIds, depIds.Length, depIds.Length);
                depIds = ndepIds;
            }
            if (depIds.Length > 0)
            {
                disjunction.Add(Restrictions.And(Restrictions.Eq("WarehouseType", (short)2), Restrictions.In("AttriToId", depIds)));
            }
            criterions.Add(junction);
            criterions.Add(disjunction);
            return Dao.FindByPage(criterions.ToArray(), null, start, count);
        }

        /// <summary>
        /// 获取某个门店的仓库地
        /// </summary>
        /// <param name="storeId">门店ID</param>
        /// <param name="loadDeps">是否读取门店下所有部门的仓库地</param>
        /// <returns></returns>
        public IList<Warehouse> FindWarehouseByStoreId(int storeId, bool loadDeps)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            Disjunction disjunction = Restrictions.Disjunction();
            disjunction.Add(Restrictions.And(Restrictions.Eq("WarehouseType", (short)1), Restrictions.Eq("AttriToId", storeId)));
            if (loadDeps)
            {
                int[] depIds = new DepartmentBLL().FindByStore(storeId).Select(d => d.DepId).ToArray();
                if (depIds.Length > 0)
                    disjunction.Add(Restrictions.And(Restrictions.Eq("WarehouseType", (short)2), Restrictions.In("AttriToId", depIds)));
            }
            criterions.Add(disjunction);
            return Dao.Find(criterions.ToArray());
        }

        public IList<Warehouse> findWarehouseByDepid(int depid)
        {
            List<ICriterion> criterions = new List<ICriterion>();

            if (depid > 0)
            {
                //criterions.Add(Restrictions.Eq("WarehouseLevel", warehouseLevel));
            }
            return null;

        }
    }
}